package com.chinasofti1.info_manager.service;

import com.chinasofti1.info_manager.dao.BaseStudentDao;
import com.chinasofti1.info_manager.domain.Student;
import com.chinasofti1.info_manager.factory.StudentDaoFactory;

public class StudentService {
    //private OtherStudentDao studentDao  = new OtherStudentDao();
    //StudentDao studentDao = new StudentDao();
        private BaseStudentDao studentDao = StudentDaoFactory.getStudentDao();
    public void deleteStudentById(String id){
        studentDao.deleteStudentById(id);
    }
    //判断学号是否存在
    public  boolean isExists(String id){//返回值为true代表不存在
        boolean exists=  false;
        //判断数组中是否有这个id
        int index =studentDao.getIndex(id);
        if(index == -1){
            exists=true;//不存在
        }
        return  exists;
    }
    public boolean addStudent(Student stu) {
        return studentDao.addStudent(stu);
    }
    public Student[] findAllStudent() {
        Student[] allStudent =  studentDao.findAllStudent();
        boolean  flag = false;//做一个开关 allStudent有值返回 没有值返回null
        for (int i = 0; i <allStudent.length ; i++) {
            Student stu = allStudent[i];
            if (stu!=null){
                flag=true;
                break;
            }
        }
        if (flag){
            return allStudent;
        }else {
            return null;
        }
    }
    public void updateStudent(String updateId, Student newStu) {
        studentDao.updateStudent(updateId,newStu);
    }
}
